package com.renjun.smweb.service.StatisticsService.impl;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.commons.componet.FileIOComponent;
import com.renjun.smweb.commons.global.FileInfoPo;
import com.renjun.smweb.commons.util.CollectionUtils;
import com.renjun.smweb.commons.util.ConnectionUtils;
import com.renjun.smweb.commons.util.ExportExcel;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.dao.StatisticsDao;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.service.StatisticsService.StatisticsService;
import com.renjun.smweb.commons.componet.FileIOComponent;
import com.renjun.smweb.commons.global.FileInfoPo;
import com.renjun.smweb.commons.util.CollectionUtils;
import com.renjun.smweb.commons.util.ExportExcel;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.dao.StatisticsDao;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.service.StatisticsService.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.renjun.smweb.commons.util.ResponseUtils.createSuccessResponseBody;

/**
 * Created by RenJun on 2017/3/3.
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private StatisticsDao statisticsDao;

    /**
     * 招生登记统计（市级）
     * @param params
     * @return
     */
    @Override
    public JSONObject registerStatisticsBySj(Map<String, Object> params) throws Exception{
        return null;
    }

    /**
     * 招生登记统计（区县托班）
     * @param params
     * @return
     */
    @Override
    public JSONObject registerStatisticsByQxTb(Map<String, Object> params) throws Exception{
        try{
            List<Map<String,Object>> resultList = statisticsDao.getRegisterStatisticsByQxTb(params);
            resultList = CollectionUtils.converMapKeyToLowerCase(resultList);
            return ResponseUtils.createSuccessResponseBody("查询成功", resultList);
        } catch (Exception e){
            return ResponseUtils.createErrorResponseBody("查询失败");
        }
    }

    /**
     * 招生登记统计（区县托班） - 导出
     * @param params
     * @return
     */
    @Override
    public JSONObject expRegisterStatisticsByQxTb(Map<String, Object> params) throws Exception{

        //获取当前学年
        Map<String,Object> currentTermYearMap = statisticsDao.getCurrentTermYear();
        String currentTermYear = String.valueOf(currentTermYearMap.get("STUDYYEARS_LOW"));

        List<Map<String,Object>> resultList = statisticsDao.getRegisterStatisticsByQxTb(params);
        FileInfoPo fileInfoPo = FileIOComponent.createTempFilePathByType(FileIOComponent.EXPORT_TYPE, "xlsx","统计分析_报名登记统计（区县托班）");
        ExportExcel ee = new ExportExcel();
        ee.createExportExcel(fileInfoPo.getFileAbsPath());
        ee.writeCurrentDataZslq(resultList,currentTermYear+"金山区托班新生登记情况统计表");
        Map<String, Object> urlMap = new HashMap<>();
        urlMap.put("url", fileInfoPo.getFileVirPath());
        JSONObject rs = ResponseUtils.createSuccessResponseBody("导出成功", urlMap);
        return rs;

    }

    /**
     * 招生登记统计（区县小班）
     * @param params
     * @return
     */
    @Override
    public JSONObject registerStatisticsByQxXb(Map<String, Object> params) throws Exception{
        try{
            List<Map<String,Object>> resultList = statisticsDao.getRegisterStatisticsByQxXb(params);
            resultList = CollectionUtils.converMapKeyToLowerCase(resultList);
            return ResponseUtils.createSuccessResponseBody("查询成功", resultList);
        } catch (Exception e){
            return ResponseUtils.createErrorResponseBody("查询失败");
        }
    }

    /**
     * 招生登记统计（区县小班） - 导出
     * @param params
     * @return
     */
    @Override
    public JSONObject expRegisterStatisticsByQxXb(Map<String, Object> params) throws Exception{

        //获取当前学年
        Map<String,Object> currentTermYearMap = statisticsDao.getCurrentTermYear();
        String currentTermYear = String.valueOf(currentTermYearMap.get("STUDYYEARS_LOW"));

        List<Map<String,Object>> resultList = statisticsDao.getRegisterStatisticsByQxXb(params);
        FileInfoPo fileInfoPo = FileIOComponent.createTempFilePathByType(FileIOComponent.EXPORT_TYPE, "xlsx","统计分析_报名登记统计（区县小班）");
        ExportExcel ee = new ExportExcel();
        ee.createExportExcel(fileInfoPo.getFileAbsPath());
        ee.writeCurrentDataZslq(resultList,currentTermYear+"金山区小班新生登记情况统计表");
        Map<String, Object> urlMap = new HashMap<>();
        urlMap.put("url", fileInfoPo.getFileVirPath());
        JSONObject rs = ResponseUtils.createSuccessResponseBody("导出成功", urlMap);
        return rs;
    }

    /**
     * 招生登记统计（学校托班）
     * @param params
     * @return
     */
    @Override
    public JSONObject registerStatisticsByXxTb(Map<String, Object> params) throws Exception{
        try{
            List<Map<String,Object>> resultList = statisticsDao.getRegisterStatisticsByXxTb(params);
            resultList = CollectionUtils.converMapKeyToLowerCase(resultList);
            return ResponseUtils.createSuccessResponseBody("查询成功", resultList);
        } catch (Exception e){
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody("查询失败");
        }
    }

    /**
     * 招生登记统计（学校托班） - 导出
     * @param params
     * @return
     */
    @Override
    public JSONObject expRegisterStatisticsByXxTb(Map<String, Object> params) throws Exception{

        //获取当前学年
        Map<String,Object> currentTermYearMap = statisticsDao.getCurrentTermYear();
        String currentTermYear = String.valueOf(currentTermYearMap.get("STUDYYEARS_LOW"));

        //获取学校名称
        Map<String,Object> map = statisticsDao.getSchoolNameByCode(params);
        String schoolName = (String) map.get("SCHOOLNAME");

        List<Map<String,Object>> resultList = statisticsDao.getRegisterStatisticsByXxTb(params);
        FileInfoPo fileInfoPo = FileIOComponent.createTempFilePathByType(FileIOComponent.EXPORT_TYPE, "xlsx","统计分析_报名登记统计（学校托班）");
        ExportExcel ee = new ExportExcel();
        ee.createExportExcel(fileInfoPo.getFileAbsPath());
        ee.writeCurrentDataZslq(resultList,currentTermYear+schoolName+"托班新生登记情况统计表");
        Map<String, Object> urlMap = new HashMap<>();
        urlMap.put("url", fileInfoPo.getFileVirPath());
        JSONObject rs = ResponseUtils.createSuccessResponseBody("导出成功", urlMap);
        return rs;
    }


    /**
     * 招生登记统计（学校小班）
     * @param params
     * @return
     */
    @Override
    public JSONObject registerStatisticsByXxXb(Map<String, Object> params) throws Exception{
        try{
            List<Map<String,Object>> resultList = statisticsDao.getRegisterStatisticsByXxXb(params);
            resultList = CollectionUtils.converMapKeyToLowerCase(resultList);
            return ResponseUtils.createSuccessResponseBody("查询成功", resultList);
        } catch (Exception e){
            return ResponseUtils.createErrorResponseBody("查询失败");
        }
    }

    /**
     * 招生登记统计（学校小班） - 导出
     * @param params
     * @return
     */
    @Override
    public JSONObject expRegisterStatisticsByXxXb(Map<String, Object> params) throws Exception{

        //获取当前学年
        Map<String,Object> currentTermYearMap = statisticsDao.getCurrentTermYear();
        String currentTermYear = String.valueOf(currentTermYearMap.get("STUDYYEARS_LOW"));

        //获取学校名称
        Map<String,Object> map = statisticsDao.getSchoolNameByCode(params);
        String schoolName = (String) map.get("SCHOOLNAME");

        List<Map<String,Object>> resultList = statisticsDao.getRegisterStatisticsByXxXb(params);
        FileInfoPo fileInfoPo = FileIOComponent.createTempFilePathByType(FileIOComponent.EXPORT_TYPE, "xlsx","统计分析_报名登记统计（学校小班）");
        ExportExcel ee = new ExportExcel();
        ee.createExportExcel(fileInfoPo.getFileAbsPath());
        ee.writeCurrentDataZslq(resultList,currentTermYear+schoolName+"小班新生登记情况统计表");
        Map<String, Object> urlMap = new HashMap<>();
        urlMap.put("url", fileInfoPo.getFileVirPath());
        JSONObject rs = ResponseUtils.createSuccessResponseBody("导出成功", urlMap);
        return rs;
    }

    /**
     * 录取统计（市级）
     * @param params
     * @return
     */
    @Override
    public JSONObject admissionStatisticsBySj(Map<String, Object> params) throws Exception{
        return null;
    }

    /**
     * 录取统计（区县托班）
     * @param params
     * @return
     */
    @Override
    public JSONObject admissionStatisticsByQxTb(Map<String, Object> params) throws Exception{
        try{
            List<Map<String,Object>> resultList = statisticsDao.getAdmissionStatisticsByQxTb(params);
            resultList = CollectionUtils.converMapKeyToLowerCase(resultList);
            return ResponseUtils.createSuccessResponseBody("查询成功", resultList);
        } catch (Exception e){
            return ResponseUtils.createErrorResponseBody("查询失败");
        }
    }

    /**
     * 录取统计（区县托班） - 导出
     * @param params
     * @return
     */
    @Override
    public JSONObject expAdmissionStatisticsByQxTb(Map<String, Object> params) throws Exception{

        //获取当前学年
        Map<String,Object> currentTermYearMap = statisticsDao.getCurrentTermYear();
        String currentTermYear = String.valueOf(currentTermYearMap.get("STUDYYEARS_LOW"));

        List<Map<String,Object>> resultList = statisticsDao.getAdmissionStatisticsByQxTb(params);
        FileInfoPo fileInfoPo = FileIOComponent.createTempFilePathByType(FileIOComponent.EXPORT_TYPE, "xlsx","统计分析_录取情况统计（区县托班）");
        ExportExcel ee = new ExportExcel();
        ee.createExportExcel(fileInfoPo.getFileAbsPath());
        ee.writeCurrentDataZslq(resultList,currentTermYear+"金山区托班新生录取情况统计表");
        Map<String, Object> urlMap = new HashMap<>();
        urlMap.put("url", fileInfoPo.getFileVirPath());
        JSONObject rs = ResponseUtils.createSuccessResponseBody("导出成功", urlMap);
        return rs;
    }

    /**
     * 录取统计（区县小班）
     * @param params
     * @return
     */
    @Override
    public JSONObject admissionStatisticsByQxXb(Map<String, Object> params) throws Exception{
        try{
            List<Map<String,Object>> resultList = statisticsDao.getAdmissionStatisticsByQxXb(params);
            resultList = CollectionUtils.converMapKeyToLowerCase(resultList);
            return ResponseUtils.createSuccessResponseBody("查询成功", resultList);
        } catch (Exception e){
            return ResponseUtils.createErrorResponseBody("查询失败");
        }
    }

    /**
     * 录取统计（区县小班） - 导出
     * @param params
     * @return
     */
    @Override
    public JSONObject expAdmissionStatisticsByQxXb(Map<String, Object> params) throws Exception{

        //获取当前学年
        Map<String,Object> currentTermYearMap = statisticsDao.getCurrentTermYear();
        String currentTermYear = String.valueOf(currentTermYearMap.get("STUDYYEARS_LOW"));

        List<Map<String,Object>> resultList = statisticsDao.getAdmissionStatisticsByQxXb(params);
        FileInfoPo fileInfoPo = FileIOComponent.createTempFilePathByType(FileIOComponent.EXPORT_TYPE, "xlsx","统计分析_录取情况统计（区县小班）");
        ExportExcel ee = new ExportExcel();
        ee.createExportExcel(fileInfoPo.getFileAbsPath());
        ee.writeCurrentDataZslq(resultList,currentTermYear+"金山区小班新生录取情况统计表");
        Map<String, Object> urlMap = new HashMap<>();
        urlMap.put("url", fileInfoPo.getFileVirPath());
        JSONObject rs = ResponseUtils.createSuccessResponseBody("导出成功", urlMap);
        return rs;
    }

    /**
     * 录取统计（学校托班）
     * @param params
     * @return
     */
    @Override
    public JSONObject admissionStatisticsByXxTb(Map<String, Object> params) throws Exception{
        try{
            List<Map<String,Object>> resultList = statisticsDao.getAdmissionStatisticsByXxTb(params);
            resultList = CollectionUtils.converMapKeyToLowerCase(resultList);
            return ResponseUtils.createSuccessResponseBody("查询成功", resultList);
        } catch (Exception e){
            return ResponseUtils.createErrorResponseBody("查询失败");
        }
    }

    /**
     * 录取统计（学校托班） - 导出
     * @param params
     * @return
     */
    @Override
    public JSONObject expAdmissionStatisticsByXxTb(Map<String, Object> params) throws Exception{

        //获取当前学年
        Map<String,Object> currentTermYearMap = statisticsDao.getCurrentTermYear();
        String currentTermYear = String.valueOf(currentTermYearMap.get("STUDYYEARS_LOW"));

        //获取学校名称
        Map<String,Object> map = statisticsDao.getSchoolNameByCode(params);
        String schoolName = (String) map.get("SCHOOLNAME");

        List<Map<String,Object>> resultList = statisticsDao.getAdmissionStatisticsByXxTb(params);
        FileInfoPo fileInfoPo = FileIOComponent.createTempFilePathByType(FileIOComponent.EXPORT_TYPE, "xlsx","统计分析_录取情况统计（学校托班）");
        ExportExcel ee = new ExportExcel();
        ee.createExportExcel(fileInfoPo.getFileAbsPath());
        ee.writeCurrentDataZslq(resultList,currentTermYear+schoolName+"托班新生录取情况统计表");
        Map<String, Object> urlMap = new HashMap<>();
        urlMap.put("url", fileInfoPo.getFileVirPath());
        JSONObject rs = ResponseUtils.createSuccessResponseBody("导出成功", urlMap);
        return rs;
    }

    /**
     * 录取统计（学校小班）
     * @param params
     * @return
     */
    @Override
    public JSONObject admissionStatisticsByXxXb(Map<String, Object> params) throws Exception{
        try{
            List<Map<String,Object>> resultList = statisticsDao.getAdmissionStatisticsByXxXb(params);
            resultList = CollectionUtils.converMapKeyToLowerCase(resultList);
            return ResponseUtils.createSuccessResponseBody("查询成功", resultList);
        } catch (Exception e){
            return ResponseUtils.createErrorResponseBody("查询失败");
        }
    }

    /**
     * 录取统计（学校小班） - 导出
     * @param params
     * @return
     */
    @Override
    public JSONObject expAdmissionStatisticsByXxXb(Map<String, Object> params) throws Exception{

        //获取当前学年
        Map<String,Object> currentTermYearMap = statisticsDao.getCurrentTermYear();
        String currentTermYear = String.valueOf(currentTermYearMap.get("STUDYYEARS_LOW"));

        //获取学校名称
        Map<String,Object> map = statisticsDao.getSchoolNameByCode(params);
        String schoolName = (String) map.get("SCHOOLNAME");

        List<Map<String,Object>> resultList = statisticsDao.getAdmissionStatisticsByXxXb(params);
        FileInfoPo fileInfoPo = FileIOComponent.createTempFilePathByType(FileIOComponent.EXPORT_TYPE, "xlsx","统计分析_录取情况统计（学校小班）");
        ExportExcel ee = new ExportExcel();
        ee.createExportExcel(fileInfoPo.getFileAbsPath());
        ee.writeCurrentDataZslq(resultList,currentTermYear+schoolName+"小班新生录取情况统计表");
        Map<String, Object> urlMap = new HashMap<>();
        urlMap.put("url", fileInfoPo.getFileVirPath());
        JSONObject rs = ResponseUtils.createSuccessResponseBody("导出成功", urlMap);
        return rs;
    }

    /**
     * 首页各类数据统计
     * @param params
     * @return
     */
    @Override
    public JSONObject homeStatistics(Map<String, Object> params) throws Exception{
        try{
            LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
            String loginType = loginInfo.getRoletype();

            //新生登记数据统计
            List<Map<String,Object>> djList = statisticsDao.getDjHomeStatistics(params);
            djList = CollectionUtils.converMapKeyToLowerCase(djList);

            //新生报名数据统计（虹口）
            List<Map<String,Object>> bmList = statisticsDao.getBmHomeStatistics(params);
            bmList = CollectionUtils.converMapKeyToLowerCase(bmList);
            if(null != djList && djList.size() > 0){
                djList.get(0).put("bmzrs",bmList.get(0).get("bmzrs"));
                djList.get(0).put("bmgfrs",bmList.get(0).get("bmgfrs"));
                djList.get(0).put("bmbgfrs",bmList.get(0).get("bmbgfrs"));
                djList.get(0).put("bmbxdjrs",bmList.get(0).get("bmbxdjrs"));
                djList.get(0).put("bmzddjrs",bmList.get(0).get("bmzddjrs"));
            }

            //新生录取数据统计
            List<Map<String,Object>> lqList = statisticsDao.getLqHomeStatistics(params);
            lqList = CollectionUtils.converMapKeyToLowerCase(lqList);
            if(null != djList && djList.size() > 0){
                djList.get(0).put("lqzrs",lqList.get(0).get("lqzrs"));
                djList.get(0).put("wlqrs",lqList.get(0).get("wlqrs"));
                djList.get(0).put("bxlqrs",lqList.get(0).get("bxlqrs"));
                djList.get(0).put("dtcrs",lqList.get(0).get("dtcrs"));
                djList.get(0).put("tclqrs",lqList.get(0).get("tclqrs"));
            }

            //招生计划数据统计
            List<Map<String,Object>> zsjhtList = statisticsDao.getZsjhStatistics(params);
            zsjhtList = CollectionUtils.converMapKeyToLowerCase(zsjhtList);
            if(null != djList && djList.size() > 0){
                djList.get(0).put("jhkbzs",zsjhtList.get(0).get("jhkbzs"));
                djList.get(0).put("jhkbtbs",zsjhtList.get(0).get("jhkbtbs"));
                djList.get(0).put("jhkbxbs",zsjhtList.get(0).get("jhkbxbs"));
                djList.get(0).put("zsedzs",zsjhtList.get(0).get("zsedzs"));
                djList.get(0).put("zsedtbs",zsjhtList.get(0).get("zsedtbs"));
                djList.get(0).put("zsedxbs",zsjhtList.get(0).get("zsedxbs"));
            }
            return ResponseUtils.createSuccessResponseBody("查询成功", djList.get(0));
        } catch (Exception e){
            return ResponseUtils.createErrorResponseBody("查询失败");
        }
    }

}
